// 用Promise来说明S O

function loadImg(src) {
  let promise = new Promise(function(resolve, reject) {
    let img = document.createElement('img');
    img.onload = function() {
      resolve(img)
    }
    img.onerror = function() {
      reject('图片加载失败')
    }
    img.src = src;
    document.body.append(img)
  })
  return promise;
}

// let src = 'https://pic.xiami.net/images/collect/276551250/436248551/da6ae2da7b7b4dd3b2da1ef132a06112_80aee14a324e4d7cba2407503cb441ad.jpg?x-oss-process=image/quality,q_80'
// let src= 'https://pic.xiami.net/images/collect/406/6/41107406_1423379607_03AB.jpg?x-oss-process=image/quality,q_80'
// let result = loadImg(src);

// result.then(function(img) {
//   alert(`width: ${img.width}`);
//   return img;
// }).then(function(img) {
//   alert(`height: ${img.height}`);
// }).catch(function(ex) {
//   alert(ex);
// })

/**
 * -----------------------------------------------------------------
 * 面试题1
 * 题目：
 * 打车时，有专车和快车，每辆车都有车牌号和名称
 * 不同车价格不同，快车1元，专车2元
 * 行程开始时，显示车辆信息
 * 行程结束时，显示打车金额(假设行程为5公里)
 */

// 车，父类
// class Car {
//   constructor(name, number) {
//     this.number = number
//     this.name = name
//   }
// }

// class Kuaiche extends Car {
//   constructor(name, number) {
//     super(name, number)
//     this.price = 1
//   }
// }

// class Zhuanche extends Car {
//   constructor(name, number) {
//     super(name, number)
//     this.price = 2
//   }
// }

// class Trip {
//   constructor(car) {
//     this.car = car
//   }
//   start() {
//     console.log(`车牌号：${this.car.number}, 车名：${this.car.name}`)
//   }
//   end() {
//     console.log(`费用为：${this.car.price * 5}元`)
//   }
// }

// let car = new Zhuanche("兰博基尼", "粤A999999")
// let trip = new Trip(car)
// trip.start()
// trip.end()

/**
 * -----------------------------------------------------------------
 * 面试题2
 * 题目：
 * 某停车场有三层，每层100个车位
 * 每个车位都能监控车的驶入和驶出
 * 车辆进入停车场前，显示每层的空余车位数量
 * 车辆进入车位时，摄像头可识别车牌号和时间
 * 车辆出来停车场时，出口显示器显示车牌号和停车时长
 */

class Floor {
  constructor(index, places){
    this.index = index
    this.places = places || []
  }
  emptyPlaceNum() {
    let num = 0
    this.places.forEach(el => {
      if (el.empty) {
        num++
      }
    })
    return num
  }
}

class Place {
  constructor() {
    this.empty = true
  }
  in() {
    this.empty = false
  }
  out() {
    this.empty = true
  }
}

class Park {
  constructor(floors) {
    this.floors = floors || []
    this.camera = new Camera()
    this.screen = new Screen()
    this.carList = {} // 存储摄像头返回的拍摄信息
  }
  emptyNum() {
    return this.floors.map(floor => {
      return `${floor.index} 层有 ${floor.emptyPlaceNum()} 个空闲车位`
    }).join('\n')
  }
  in(car) {
    // 通过摄像头获取信息
    const info = this.camera.shot(car)
    // 停到某个停车位 去随机数
    const i = parseInt(Math.random() * 100)
    const place = this.floors[0].places[i]
    console.log(i);
    place.in()
    info.place = place
    // 记录信息
    this.carList[car.number] = info
  }
  out(car) {
    // 获取信息
    const info = this.carList[car.number]
    // 将车位清空
    const place = info.place
    place.out()
    // 显示时间
    this.screen.show(car, info.inTime)
    // 清空记录
    delete this.carList[car.number]
  }
}

class Screen {
  show(car, inTime) {
    console.log('车牌号:', car.number)
    console.log('停车时间:', Date.now() - inTime)
  }
}

class Camera {
  shot(car) {
    return {
      num: car.number,
      inTime: Date.now()
    }
  }
}

class Car {
  constructor(number) {
    this.number = number
  }
}

const floors = []
for(let i = 0; i < 3; i++) {
  const places = []
  for(let j = 0; j < 100; j++) {
    places[j] = new Place();
  }
  floors[i] = new Floor(i+1, places)
}
const park = new Park(floors)
// console.log(floors);
// 初始化车
let car1 = new Car(100)
let car2 = new Car(200)
let car3 = new Car(300)

console.log("第一辆车进入")
park.in(car1)
console.log(park.emptyNum())
console.log("第二辆车进入")
park.in(car2)
console.log(park.emptyNum())
console.log("第一辆车离开")
park.out(car1)
console.log("第二辆车离开")
park.out(car2)
console.log("第三辆车进入")
console.log(park.emptyNum())
park.in(car3)
console.log("第三辆车离开")
park.out(car3)
console.log(park.emptyNum());